// pages/pay/index.js
const {formatTime, showtoas} = require("../../utils/util");
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },
    //所有订单
    allorder:[],
    //单个订单
    orderinfo:{
        number:'',
        price:0,
        time:'',
        ispay:false
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        //取出地址
        let address=wx.getStorageSync('address');
        console.log(address);
        //取出用户信息
        let usinfo=wx.getStorageSync('userinfo');
        //取出购物车的商品
        let carlis=wx.getStorageSync('car') || [];
        //过滤选中的
        let filcar=carlis.filter((item)=>{
            return item.checked=true;
        });
        let totalprice=0;
        let totalcount=0;
        //遍历数组
        filcar.forEach((item)=>{
            //总价
            totalprice+=item.number*item.goods_price;
            //数量
            totalcount+=item.number;
        });
        this.setData({
            address:address,
            usinfo:usinfo,
            payshop:filcar,
            totalprice:totalprice,
            totalcount:totalcount
        })
    },
    //结算
    async handpay(e) {
        //取出原来的订单
        let oldorder=wx.getStorageSync('orderinfo');
        //不等于空就先覆盖
        if(oldorder!=''){
            this.allorder=oldorder;
        }
        //12未的数字
        let ordernum = this.authorder(100000000000, 999999999999);
        this.orderinfo.number = ordernum;
        //总价
        this.orderinfo.price = e.currentTarget.dataset.totalmoney;
        //当前时间
        this.orderinfo.time = formatTime(new Date());
        //调用封装的showtoas
        let res = await showtoas({
            mask: true,
            title: '是否继续支付？'
        });
        //确认
        if(res.confirm==true){
            //加载
           wx.showLoading();
           //关闭加载
           setTimeout(()=>{
               wx.hideLoading();
           },3000);
           //支付成功
           this.orderinfo.ispay = true;
        }else{//取消
            //加载
            wx.showLoading();
            //关闭加载
            setTimeout(()=>{
                wx.hideLoading();
            },3000);
            //支付失败
            this.orderinfo.ispay = false;
        }
        //放到原数组
        this.allorder.push(this.orderinfo);
        //放入缓存
        wx.setStorageSync('orderinfo', this.allorder);
        //移动购物车的内容
        wx.removeStorageSync('car');
        //跳转到订单huan'guan
        wx.redirectTo({
            url:'/pages/order/index'
        })
    },
    authorder(Min,Max){
        //随机字母
        let letter=[];
        for (var i=0;i<2;i++){
            var ranum=Math.ceil(Math.random()*25);
            letter.push(String.fromCharCode(65+ranum));
        }
        letter=letter.join('');
        letter=letter.toString();
        //随机12位数字
        var Range = Max - Min;
        var Rand = Math.random();
        let num=Min + Math.round(Rand * Range);
        //获取年月日
        let time=formatTime(new Date());
        let nyr=time.substring(0,10);
        //把年月日里面/去掉
        nyr = nyr.replace('"','').replace(/[/]/g,'');
        return letter+nyr+num;
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
